/*
 * Copyright © 2009 Perseus Project - Tufts University <http://www.perseus.tufts.edu>
 *
 * This file is part of UniCollatorPerseus.
 *
 * OcroPerseus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * OcroPerseus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with OcroPerseus.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.himeros.text;

import eu.himeros.util.DbManager;
import eu.himeros.util.IoManager;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.TreeSet;
import java.util.Vector;

/**
 * Count Characters.
 * Main class.<br/>
 * Pipe with: CharWriter (CharWriter -> charListFile.txt -> CharCounter)<br/>
 * uses: DbManager, IoManager<br/>
 * 
 * @author Federico Boschetti <federico.boschetti.73@gmail.com>
 */
public class CharCounter {
    BufferedReader br=null;
    BufferedReader allCharBr=null;
    BufferedWriter bw=null;
    Hashtable<String,Integer> ht=new Hashtable<String,Integer>(500);
    DbManager db=null;
    Vector<String> allCharsV=new Vector<String>(1000);

    /**
     *
     * @param args
     */
    public CharCounter(String[] args){
        br=IoManager.getReader(args[0]);
        allCharBr=IoManager.getReader(args[1]);
        bw=IoManager.getWriter(args[2]);
        db=new DbManager();
        try{
            readAllChars();
            countChars();
            write();
            bw.close();
        }catch(Exception e){e.printStackTrace();}
    }

    /**
     *
     */
    private void countChars(){
        String line=null;
        String charx="";
        int num=0;
        try{
            while((line=br.readLine())!=null){
                for(int i=0;i<line.length();i++){
                    charx=""+line.charAt(i);
                    if(ht.get(charx)==null){
                        ht.put(charx, new Integer(1));
                    }else{
                        num=ht.get(charx).intValue()+1;
                        ht.put(charx,new Integer(num));
                    }
                }
            }
        }catch(Exception e){e.printStackTrace();}
    }

    /**
     *
     */
    private void readAllChars(){
        String line=null;
        String charx="";
        try{
            while((line=allCharBr.readLine())!=null){
                charx=line.substring(0,line.indexOf(' '));
                allCharsV.add(charx);
            }
        }catch(Exception ex){ex.printStackTrace();}
    }

    /**
     *
     */
    private void write(){
        TreeSet<String> ts=new TreeSet<String>();
        Enumeration<String> keys=ht.keys();
        try{
            while(keys.hasMoreElements()){ts.add(keys.nextElement());}
            db.stmt.execute("create table tab(t1 varchar,t2 int)");
            db.stmt.execute("create index idx on tab(t2)");
            db.prepareStatement("insert into tab values(?,?)");
            for(String charx:ts){
                db.pstmt.setString(1,charx);
                db.pstmt.setInt(2, ht.get(charx));
                db.pstmt.executeUpdate();
                System.out.println(charx);
            }      
            db.rst=db.stmt.executeQuery("select * from tab order by t2 desc");
            while(db.rst.next()){
                allCharsV.remove(db.rst.getString(1));
                char cx=db.rst.getString(1).charAt(0);
                
                bw.write(Integer.toHexString(cx)+" "+cx + " " + db.rst.getInt(2));bw.newLine();
            }
            for(String s:allCharsV){
                char cx=s.charAt(0);
                bw.write(Integer.toHexString(cx)+" "+s+" "+0);bw.newLine();
            }
        }catch(Exception ex){ex.printStackTrace();}
    }

    /**
     *
     * @param args
     */
    public static void main(String[] args){
        if(args==null||args.length==0){
            System.out.println("It counts how many occurrences of a character are contained in the input file");
            System.out.println("Usage: java eu.himeros.ocrtrainer.CharCounter <textFile> <charListFile> <resultFile>");
            System.out.println("<textFile> contains the text to be processed");
            System.out.println("<charListFile> contains the list of characters to be count");
            System.out.println("<resultFile> contains the result");
            System.exit(1);
        }
        CharCounter cc=new CharCounter(args);
    }

}
